package richbar.com.richbar.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.common.base.BaseActivity;
import com.common.base.BaseFragment;
import com.common.help.RecycleViewHelp;
import com.common.util.LogUtil;
import com.common.util.StringUtil;
import com.okhttp.network.HttpUtils;
import com.okhttp.network.MyGson;

import java.util.ArrayList;
import java.util.List;

import richbar.com.richbar.R;
import richbar.com.richbar.activity.product.ProductListAc;
import richbar.com.richbar.adapter.ClassifyAdapter;
import richbar.com.richbar.bean.CateLeftResult;
import richbar.com.richbar.fragment.classify.BaseNewClassifyFragment;
import richbar.com.richbar.util.NetWorkUtil;
import richbar.com.richbar.util.Util;

/**
 * Created by Administrator on 2018/10/13.
 * 分类
 */

public class ClassificationFragment extends BaseFragment{
    private RecyclerView recyclerView;
    private ViewPager viewPager;
    private ClassifyAdapter classifyAdapter;
    private RecycleAdapter recycleAdapter;
    private LinearLayout input_body;
    private TextView input_et;


    @Override
    protected View onCreate(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_classification, container, false);
        recyclerView = view.findViewById(R.id.recycleView);
        viewPager = view.findViewById(R.id.viewpager);
        input_body = view.findViewById(R.id.input_body);
        input_et = view.findViewById(R.id.input_et);
        input_et.setText("电烤鸡胸肉片");
        input_body.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                start(ProductListAc.class, new BaseActivity.OnBundleListener() {
                    @Override
                    public Bundle onSetBundle(Bundle bundle) {
                        bundle.putString("goodsName", StringUtil.getStr(input_et));
                        return bundle;
                    }
                });
            }
        });
        return view;
    }

    @Override
    protected void initData() {
        //left
        RecycleViewHelp.setManager(recyclerView,getActivity());
        recycleAdapter = new RecycleAdapter(R.layout.item_classify_left, new ArrayList<CateLeftResult.DataEntity.ListEntity>());
        RecycleViewHelp.setManager(recyclerView,getActivity());
        recyclerView.setAdapter(recycleAdapter);
        getData();
    }

    private void getData() {
        NetWorkUtil.getCateLeft(Util.md5(),new HttpUtils.HttpCallback() {
            @Override
            public void onSuccess(String data) {
                LogUtil.e("PPPPPPPP",data);
                final CateLeftResult result = MyGson.getInstance().fromJson(data, CateLeftResult.class);
                if (checkIsOk(result)) {
                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            recycleAdapter.setNewData(result.getData().getList());
                            final List<Fragment> fragments = new ArrayList<>();
                            for (int i = 0; i < result.getData().getList().size(); i++) {
                                fragments.add(BaseNewClassifyFragment.newInstance(result.getData().getList().get(i).getId()));
                            }
                            classifyAdapter = new ClassifyAdapter(fragments,getChildFragmentManager());
                            viewPager.setAdapter(classifyAdapter);
                            viewPager.setOffscreenPageLimit(3);
                        }
                    });
                }
            }
        });
    }





    class RecycleAdapter extends BaseQuickAdapter<CateLeftResult.DataEntity.ListEntity> {
        private int clickPos = 0;

        public RecycleAdapter(int layoutResId, List<CateLeftResult.DataEntity.ListEntity> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(final BaseViewHolder holder, CateLeftResult.DataEntity.ListEntity bean) {
            holder.setText(R.id.name,bean.getName());
            View view = holder.getView(R.id.view);
            TextView tv = holder.getView(R.id.name);
            if (clickPos == holder.getAdapterPosition()){
                holder.itemView.setBackgroundResource(R.color.gray_f6);
                tv.setTextColor(R.color.red);
                tv.setTextColor(ContextCompat.getColor(getActivity(),R.color.red));
                view.setVisibility(View.VISIBLE);
            }else {
                holder.itemView.setBackgroundResource(R.color.white);
                holder.setTextColor(R.id.name,R.color.black);
                view.setVisibility(View.GONE);
                tv.setTextColor(ContextCompat.getColor(getActivity(),R.color.black));
            }

            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    clickPos = holder.getAdapterPosition();
                    notifyDataSetChanged();
                    viewPager.setCurrentItem(clickPos,false);
                }
            });
        }
    }
}
